﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace UMS.TransportLayer
{
    public class ExternalIP
    {
        public static IPAddress GetExternalIP()
        {
            StreamReader reader;
            HttpWebRequest httpWebRequest;
            HttpWebResponse httpWebResponse;

            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://whatismyip.org/");
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                reader = new StreamReader(httpWebResponse.GetResponseStream());
                string IP = System.Text.RegularExpressions.Regex.Match(reader.ReadToEnd(),
                    @"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})").Groups[1].Value;
                IPAddress i = IPAddress.Parse(IP);
                return i;
            }
            catch
            {
                return null;
            }
        }
    }
}
